package soko;

import soko.control.TUI;
import soko.view.TUIView;

/**
 * This class contains the main method of the sokoban solver.
 */
public class Sokoban {

	public static final String HELPMSG = "\r\nSokoban file [-finddeadlock] " + 
			"[-nodeadlockremoval] [-countsolutions] [-lurd lurdstring]\r\n file: the path to a screen file.\r\n" +
			" -finddeadlock : enable to look for the (shortest) trace to an unsolvable state.\r\n" +
			" -nodeadlockremoval : do not remove deadlock states (results in slower computations with more states).\r\n"
			+ " -countsolutions : count the number of reachable solution states.\r\n"
			+ " -lurd lurdstring: apply the lurdstring (consisting of l u r and d characters) to the initial state before solving";

	/**
	 * Runs the sokoban solver
	 * @param args needs one obligatory argument; a path to a screen.
	 * Can have four optional arguments.
	 * @see Sokoban.HELPMSG for optional arguments.
	 */
	public static void main(String[] args) {
		boolean findDeadlock = false;
		boolean removeDeadlocks = true;
		boolean countSolutions = false;
		String lurd = null;
		if (args.length == 0) {
			TUIView.debugAndExit1("Need at least one argument; the input file."
					+ HELPMSG);
		}
		for(int i=1;i<args.length;i++){
			if(args[i].toLowerCase().equals("-d") || args[i].toLowerCase().equals("-finddeadlock")){
				findDeadlock = true;
			} else if(args[i].toLowerCase().equals("-nodeadlockremoval")){
				removeDeadlocks = false;
			} else if(args[i].toLowerCase().equals("-countsolutions")){
				countSolutions = true;
			} else if(args[i].toLowerCase().equals("-lurd")){
				i++;
				if(i>=args.length || args[i].startsWith("-")){
					TUIView.debugAndExit1("The -lurd argument needs to be followed by a lurd string."+HELPMSG);
				}
				lurd = args[i];
			} else {
				TUIView.debugAndExit1("Unrecognised argument: "+args[i]+HELPMSG);
			}
		}
		if(findDeadlock && !removeDeadlocks){
			TUIView.debugAndExit1("-finddeadlock and -nodeadlockremoval cannot be combined"+HELPMSG);
		} else if(countSolutions && findDeadlock){
			TUIView.debugAndExit1("-countSolutions and -findDeadlock cannot be combined"+HELPMSG);
		}
		new TUI(args[0]).init(findDeadlock, removeDeadlocks, countSolutions, lurd);
	}
}